package com.dang.apj.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.dang.apj.dao.CategoryDao;
import com.dang.apj.model.Category;
import com.dang.apj.util.JDBCUtil;

public class CategoryDaoImpl implements CategoryDao {

	private static final String FIND_ALL = "select * from d_category";

	@Override
	public List<Category> findAll() throws Exception {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Category> cats = new ArrayList<Category>();
		try{
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(FIND_ALL);
			rs = ps.executeQuery();
			while(rs.next()){
				Category c = new Category();
				c.setDescription(rs.getString("description"));
				c.setEnName(rs.getString("en_name"));
				c.setId(rs.getInt("id"));
				c.setName(rs.getString("name"));
				c.setParentId(rs.getInt("parent_id"));
				cats.add(c);
			}
		}finally{
			JDBCUtil.close(rs);
			JDBCUtil.close(ps);
			JDBCUtil.close(conn);
		}
		
		
		return cats;
	}

}
